<?php
/**
*小区销售数据
*/
require("../include/init.php");
// print_r($data);
// exit;

date_default_timezone_set('Asia/Shanghai');
//获取销量统计
// $sql="select * from xssj where xid=1";
$mysql=mysql::getIns();
// $row=$mysql->getOne($sql);
//获取进店频次等信息
// $sql="select * from xszj where xid=1";
// $mysql=mysql::getIns();
// $data=$mysql->getOne($sql);



/*************************************

	进店频次(每天统计一次,今天累计人数的进店频次)


************************************/
//统计当天累计人数数


$sql="select areaname from area";
$area=$mysql->getAll($sql);
foreach($area as $v){
  $address=$v['areaname'];
  $t = time();
  $start = mktime(0,0,0,date("m",$t),date("d",$t),date("Y",$t));
  $end = mktime(23,59,59,date("m",$t),date("d",$t),date("Y",$t));
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $start and timestamp < $end and shop='". $address ."'";
  $allday=$mysql->getOne($sql);

  //统计进店频次为2次的
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $start and timestamp < $end  and visittimes='2' and shop='". $address ."'";
  $visittwo=$mysql->getOne($sql);
  // echo $visittwo;
  // exit;

  //统计进店频次为3次
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $start and timestamp < $end  and visittimes='3' and shop='". $address ."'";
  $visitthree=$mysql->getOne($sql);
  // echo $visitthree;
  // exit;



  //统计进店频次为3次以上
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $start and timestamp < $end  and visittimes > '3' and shop='". $address ."'";
  $visitmore=$mysql->getOne($sql);
  // echo $visitmore;
  // exit;

  //计算频次

  //两次
  $vtwo=ceil(($visittwo/$allday)*100);
  // echo $vtwo;
  // exit;
  //三次
  $vthree=ceil(($visitthree/$allday)*100);
  // echo $vthree;
  // exit;

  //三次以上
  $vmore=ceil(($visitmore/$allday)*100);
  // echo $vmore;
  // exit;

  //一次
  $vone=100-$vtwo-$vthree-$vmore;
  // echo $vone;
  // exit;
  switch ($address) {
    case '义乌市博奥汽车销售服务有限公司':
      $hash='SubArea_SaleDirector_ywba';
      break;
    case '广东粤奥汽车销售服务有限公司':
      $hash='SubArea_SaleDirector_gdya';
      break;
    case '辽宁鑫迪汽车销售服务有限公司':
      $hash='SubArea_SaleDirector_lnxd';
      break;
    case '江苏天奥汽车销售服务有限公司':
        $hash='SubArea_SaleDirector_jsta';
        break;
    case '成都新元素雅麓汽车销售服务有限公司':
      $hash='SubArea_SaleDirector_cdyl';
      break;
    case '云南驰泰汽车销售服务有限公司':
      $hash='SubArea_SaleDirector_ynct';
      break;
    case '宁夏奥立升汽车销售服务有限公司':
      $hash='SubArea_SaleDirector_nxals';
      break;
    case '陕西奥诚汽车销售服务有限公司':
      $hash='SubArea_SaleDirector_sxac';
      break;
    case '北京博瑞祥星汽车销售有限公司':
      $hash='SubArea_SaleDirector_brxx';
      break;
  }
  $redis = new Redis();
    //连接服务器
    $redis->connect("localhost");
    //授权
    $redis->auth("redis=pwd@2016");
  $redis->hmset($hash,
  array(
    'allday'=>$allday,
    'vtwo'=>$vtwo,
    'vthree'=>$vthree,
    'vmore'=>$vmore,
    'vone'=>$vone,
  ));
}
